home *** CD-ROM | disk | FTP | other *** search
/ Pirate Informatique 31 / Pirate Informatique N°31.iso / js / bgSlider.js < prev    next >
Text File  |  2011-09-15  |  6KB  |  246 lines

  1. ;(function($,undefined){
  2.     var _timer=[],
  3.         _fw=window._fw=$.fn._fw=function(_){
  4.             var i,name=[]
  5.             for(i in _)
  6.                 if(_.hasOwnProperty(i))
  7.                     name.push(i)
  8.             $(this).each(function(){
  9.                 for(var i=0,opt;i<name.length;i++)
  10.                     if(_fw.meth[name[i]])                        
  11.                         opt=$.extend(clone(_fw.meth[name[i]]),_[name[i]]),
  12.                         opt.init.call($(this).data(name[i],opt),opt)
  13.             })
  14.             return this
  15.         },
  16.         _meth=_fw.meth={},
  17.         _hlp=_fw.hlp={
  18.             clone:function(obj){
  19.                 if(!obj||typeof obj!=typeof {})
  20.                     return obj
  21.                 if(obj instanceof Array)
  22.                     return [].concat(obj)
  23.                 var tmp=new obj.constructor(),
  24.                     i
  25.                 for(i in obj)
  26.                     if(obj.hasOwnProperty(i))
  27.                         tmp[i]=clone(obj[i])
  28.                 return tmp
  29.             },
  30.             srlz:function(str){
  31.                 if(!str)
  32.                     return {}
  33.                 str=str.split(/[\/&]/)
  34.                 for(var i=0,tmp,ret={};i<str.length;i++)
  35.                     if(str[i])
  36.                         tmp=str[i].split('='),
  37.                         ret[tmp[1]?tmp[0]:i]=tmp[1]?tmp[1]:tmp[0]
  38.                 return ret
  39.             },
  40.             dStr:function(obj){
  41.                 var key,
  42.                     ret=''
  43.                 for(key in obj)
  44.                     if(obj.hasOwnProperty(key))
  45.                         if(key/1==''/1)
  46.                             ret+=!ret?obj[key]+'/':obj[key]
  47.                         else
  48.                             ret+=!ret?key+'='+obj[key]+'&':key+'='+obj[key]
  49.                 return ret
  50.             }
  51.         },
  52.         clone=_hlp.clone
  53.         
  54. $.fn.extend({
  55.     bgSlider:function(opt){
  56.         opt=opt||{}
  57.         opt={bgSlider:opt}
  58.         this._fw(opt)
  59.     }
  60. })
  61.  
  62. $.extend(_fw.meth,{
  63.     bgSlider:{
  64.             slideshow:false,
  65.             duration:1500,
  66.             easing:'',
  67.             preload:false,
  68.             pagination:false,
  69.             pagActiveCl:'current',
  70.             pagEv:'click',
  71.             pagArea:'a',
  72.             current:0,
  73.             currN:0,
  74.             method:'fit',
  75.             altCSS:{},
  76.             padding:0,
  77.             preload:false,
  78.             spinner:false,
  79.             minSpinnerWait:150,
  80.             preloadFu:function(){
  81.                 var opt=this,
  82.                     img=$('<img>')
  83.                             .css({position:'absolute',left:'-999%'})
  84.                             .appendTo('body'),
  85.                     num=opt.images.length
  86.                 ;(function(){
  87.                     if(num)
  88.                         img    .load(arguments.callee)
  89.                             .attr({src:opt.images[--num]})
  90.                     else
  91.                         img.remove()                    
  92.                 })()
  93.             },
  94.             pagsFu:function(){
  95.                 var opt=this,
  96.                     pags=opt.pags=$(opt.pagination+' li')
  97.                 if(!opt.images)
  98.                     opt.images=[],
  99.                     pags.each(function(i){
  100.                         opt.images.push($('a',this).attr('href'))
  101.                     })
  102.                 pags.find(opt.pagArea).each(function(i){
  103.                     $(this).data({num:i})
  104.                 })
  105.                 pags.parent()
  106.                     .delegate(opt.pagination+':not(.'+opt.pagActiveCl+')'+(opt.pagArea?' '+opt.pagArea:''),opt.pagEv,function(){
  107.                         var th=$(this)
  108.                         opt.changeFu(th.data('num'))
  109.                         opt.pags.not(th.parent().addClass(opt.pagActiveCl)).removeClass(opt.pagActiveCl)    ;
  110.                         Cufon.replace('.pagination li', { fontFamily: 'Ubuntu', hover:true });
  111.                         return false
  112.                     })
  113.             },
  114.             preFu:function(){
  115.                 var opt=this
  116.                 opt.img
  117.                     .css({
  118.                         position:'absolute',
  119.                         left:0,
  120.                         top:0
  121.                         })
  122.                     .css(opt.altCSS)
  123.                     .attr({src:opt.images[opt.current]})                    
  124.                 opt.img.each(function(){
  125.                     var _f=function(){
  126.                                 opt.resizeFu()
  127.                                 opt.img.data({width:opt.img.width(),height:opt.img.height()})                        
  128.                             }
  129.                     if(this.complete)
  130.                         _f()
  131.                     else
  132.                         $(this)
  133.                             .load(_f)
  134.                 })
  135.                     
  136.                 opt.holder
  137.                     .css({
  138.                         position:'fixed',
  139.                         left:0,
  140.                         right:0,
  141.                         top:0,
  142.                         bottom:0,
  143.                         zIndex:-1
  144.                         })
  145.                     .append(opt.img)
  146.                 if(opt.spinner)
  147.                     opt.spinner.hide()
  148.             },
  149.             resizeFu:function(){
  150.                 var opt=this,
  151.                     img=opt.img,
  152.                     w=opt.wi,
  153.                     h=opt.he,
  154.                     l=img.css('left'),
  155.                     t=img.css('top'),
  156.                     bw=document.body.offsetWidth-opt.padding,
  157.                     bh=document.body.offsetHeight,
  158.                     k=w/h
  159.                 
  160.             },
  161.             changeFu:function(n){
  162.                 var opt=this
  163.                 if(n==opt.currN)
  164.                     return false
  165.                 opt.currN=n
  166.                 opt.showFu(opt.images[n])
  167.             },
  168.             nextFu:function(){
  169.                 var opt=this,
  170.                     n=opt.currN
  171.                 opt.changeFu(++n<opt.images.length?n:n=0)
  172.                 opt.pags.eq(n).addClass(opt.pagActiveCl).siblings().removeClass(opt.pagActiveCl)
  173.             },
  174.             prevFu:function(){
  175.                 var opt=this,
  176.                     n=opt.currN
  177.                 opt.changeFu(--n>=0?n:n=opt.images.length-1)
  178.                 opt.pags.eq(n).addClass(opt.pagActiveCl).siblings().removeClass(opt.pagActiveCl);
  179.                 Cufon.replace('.pagination li', { fontFamily: 'Ubuntu', hover:true });
  180.             },
  181.             showFu:function(src){
  182.                 var opt=this,
  183.                     clone=opt.clone=opt.img.clone(true)
  184.                 if(opt.slideshow)
  185.                     clearInterval(_timer[0])
  186.                 clone
  187.                     .css({
  188.                          opacity:0,
  189.                          left:0,
  190.                          top:0
  191.                          })
  192.                     .appendTo(opt.holder)    
  193.                     .width(opt.img.width())
  194.                     .load(function(){
  195.                         var th=$(this)
  196.                         opt.holder.find('>*').stop()                        
  197.                         setTimeout(function(){
  198.                             opt.spinner.hide()
  199.                             opt.wi=th.width()
  200.                             opt.he=th.height()
  201.                             clone
  202.                                 .stop()
  203.                                 .animate({
  204.                                         opacity:1
  205.                                         },{
  206.                                         duration:opt.duration,
  207.                                         easing:opt.easing,
  208.                                         complete:function(){
  209.                                             var tmp=opt.holder.find('img')
  210.                                             opt.img=$(this)
  211.                                             tmp.not(tmp.last()).remove()
  212.                                             opt.resizeFu()
  213.                                         }
  214.                                         })
  215.                         },opt.minSpinnerWait)
  216.                     })
  217.                     .attr({src:src})
  218.                     opt.spinner.show()
  219.                     if(opt.slideshow)
  220.                         _timer[0]=setInterval(function(){
  221.                             opt.nextFu()
  222.                         },opt.slideshow)
  223.             },
  224.             init:function(opt){
  225.                 var holder=opt.holder=this,
  226.                     img=opt.img=$('<img>')
  227.                 if(opt.pagination)
  228.                     opt.pagsFu()
  229.                 if(opt.spinner)
  230.                     opt.spinner=$(opt.spinner)
  231.                 opt.preFu()
  232.                 if(opt.preload)
  233.                     opt.preloadFu()
  234.                 window.onresize=function(){
  235.                     opt.resizeFu()
  236.                 }
  237.                 if(opt.slideshow)
  238.                     _timer[0]=setInterval(function(){
  239.                         opt.nextFu()
  240.                     },opt.slideshow)
  241.                 holder.data({opt:opt})
  242.             }
  243.         }
  244. })
  245. })(jQuery)
  246.